package com.yeyaomai.dksns.flowis.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Order;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.yeyaomai.dksns.flowis.domain.DkUserInfo;
import com.yeyaomai.dksns.flowis.domain.LatestRefer;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisQueryOrder;
import com.yeyaomai.dksns.flowis.domain.flowisdetail.FlowisStatDetailDTO;
import com.yeyaomai.dksns.flowis.service.FlowisQueryService;
import com.yeyaomai.dksns.flowis.service.TodayService;
import com.yeyaomai.dksns.flowis.service.result.DynamicXml;
import com.yeyaomai.dksns.flowis.util.Globals;
import com.yeyaomai.dksns.flowis.util.VelocityToolDateUtils;

@Controller
@RequestMapping("/current/*")
public class OnlineController {

	@Autowired
	FlowisQueryService flowisQueryService;
	
	@Autowired
	private TodayService todayService;

	@RequestMapping(value = "/online")
	public ModelAndView getCurrentOnline(HttpServletRequest request,
			HttpServletResponse response, FlowisQueryOrder order) {
		Date date = new Date();
		order.addSort("access_date", Order.DESCENDING.toString());
		ModelAndView view = new ModelAndView("online-status");
		view.addObject("date", Globals.FORMAT_TYPE_A.format(date));
		view.addObject("datexml", VelocityToolDateUtils.format.format(date));
		Page<FlowisStatDetailDTO> result = flowisQueryService
				.getOnlineStatus(order);
		Map<String, Integer> map = new HashMap<String, Integer>();
		map = flowisQueryService.getDateByOnlineCount(date);
		view.addObject("pv", map.get("pv"));
		view.addObject("ip", map.get("ip"));
		view.addObject("result", result);
		view.addObject("number", order.getNumber());
		return view;
	}

	@RequestMapping(value = "/onlinexml")
	public void onlineStatusXml(HttpServletResponse response,
			@RequestParam("datexml") String datexml) throws IOException,
			ParseException {
		// System.out.println(datexml);
		Date date = VelocityToolDateUtils.format.parse(datexml);
		List<FlowisStatDetailDTO> flowList = new ArrayList<FlowisStatDetailDTO>();
		flowList = flowisQueryService.getDateByOnline(date);
		String xml = DynamicXml.getOnline(flowList, date);
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().print(xml);
	}

	@RequestMapping(value = "/latestrefer")
	public ModelAndView latestrefer(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView view = new ModelAndView("from-revent");
		List<LatestRefer> latestReferList = flowisQueryService.getLatestRefer();
		Map<String, Integer> map = new HashMap<String, Integer>();
		map = flowisQueryService.getDateByOnlineCount(new Date());
		view.addObject("date", Globals.FORMAT_TYPE_A.format(new Date()));
		view.addObject("pv", map.get("pv"));
		view.addObject("ip", map.get("ip"));
		view.addObject("latestReferList", latestReferList);
		
		return view;
	}
	
	@RequestMapping(value = "/active")
	public ModelAndView active(HttpServletRequest request,
			HttpServletResponse response) throws ParseException {

		ModelAndView view = new ModelAndView("active-week");
		String username = request.getParameter("username");
		 String dkcode  = request.getParameter("dkcode");
		DkUserInfo userInfo = new DkUserInfo();
		userInfo.setUsername(username);
		if(dkcode!= null)
				{
			if(!dkcode.equals(""))
			{
				userInfo.setDkcode(Integer.valueOf(dkcode));
			}
				}
		Date date = new Date();
		SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd");
		Integer endTime = (int) (ff.parse(ff.format(date)).getTime() / 1000) - 86400;
		userInfo.setDate(endTime);
		List<DkUserInfo> list = todayService.getUserInfoByTime(userInfo);
		view.addObject("username", username);
		view.addObject("dkcode", dkcode);
		view.addObject("content", list);
		return view;
	}
}
